package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.CluePoolDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {

    @Autowired
    private ClueService clueService;

    @PostMapping
    public Result add(@RequestBody Clue clue){
        log.info("添加线索:{}",clue);
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
        return Result.success();
    }

    /**
     * 线索列表查询
     *
     */
    @GetMapping
    public Result list(ClueDto clueDto){
        log.info("查询线索:{}",clueDto);
        PageResult<Clue> pageResult = clueService.listCules(clueDto);
        return Result.success(pageResult);
    }

    /**
     *分配线索
     */
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId,@PathVariable Integer userId){
        log.info("分配线索:{},{}",clueId,userId);
        Clue clue=new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }

    /**
     * 根据ID查询线索
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public Result  getById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        Clue clue =clueService.getClueById(id);
        return Result.success(clue);
    }

    /**
     * 线索跟进
     * @param clue
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Clue clue){
        log.info("修改线索:{}",clue);
        clueService.trackClue(clue);
        return Result.success();
    }

    /**
     *将线索标记
     */

    @PutMapping("/false/{id}")
    public Result markFalse(@PathVariable Integer id,@RequestBody MarkFalseClueDto markFalseClueDto){
        log.info("将线索标记为伪线索,id:{} {}",id,markFalseClueDto);
        clueService.markFalseClue(id,markFalseClueDto);
        return Result.success();
    }


    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        log.info("将线索转为商机,id:{}",id);
        clueService.toBusiness(id);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result getPool(CluePoolDto cluePoolDto){
        log.info("查询线索池:{}",cluePoolDto);
        PageResult<Clue> pageResult = clueService.getCluePool(cluePoolDto);
        return Result.success(pageResult);
    }

}
